﻿using System;
using System.Reflection;
using System.Configuration;
namespace YW.DALFactory
{
	/// <summary>
    /// Abstract Factory pattern to create the DAL。
    /// 如果在这里创建对象报错，请检查web.config里是否修改了<add key="DAL" value="YW.SQLServerDAL" />。
	/// </summary>
	public sealed class DataAccess 
	{
        private static readonly string AssemblyPath =  string.IsNullOrEmpty(ConfigurationManager.AppSettings["DAL"])?"YW.SQLServerDAL":ConfigurationManager.AppSettings["DAL"];        
		public DataAccess()
		{ }

        #region CreateObject 

		//不使用缓存
        private static object CreateObjectNoCache(string AssemblyPath,string classNamespace)
		{		
			try
			{
				object objType = Assembly.Load(AssemblyPath).CreateInstance(classNamespace);	
				return objType;
			}
			catch//(System.Exception ex)
			{
				//string str=ex.Message;// 记录错误日志
				return null;
			}			
			
        }
		//使用缓存
		private static object CreateObject(string AssemblyPath,string classNamespace)
		{			
			object objType = DataCache.GetCache(classNamespace);
			if (objType == null)
			{
				try
				{
					objType = Assembly.Load(AssemblyPath).CreateInstance(classNamespace);					
					DataCache.SetCache(classNamespace, objType);// 写入缓存
				}
				catch//(System.Exception ex)
				{
					//string str=ex.Message;// 记录错误日志
				}
			}
			return objType;
		}
        #endregion

        #region 泛型生成
        ///// <summary>
        ///// 创建数据层接口。
        ///// </summary>
        //public static t Create(string ClassName)
        //{

        //    string ClassNamespace = AssemblyPath +"."+ ClassName;
        //    object objType = CreateObject(AssemblyPath, ClassNamespace);
        //    return (t)objType;
        //}
        #endregion

        #region CreateSysManage
        public static YW.IDAL.ISysManage CreateSysManage()
		{
			//方式1			
			//return (YW.IDAL.ISysManage)Assembly.Load(AssemblyPath).CreateInstance(AssemblyPath+".SysManage");

			//方式2 			
			string classNamespace = AssemblyPath+".SysManage";	
			object objType=CreateObject(AssemblyPath,classNamespace);
            return (YW.IDAL.ISysManage)objType;		
		}
		#endregion
             
        
   
		/// <summary>
		/// 创建YW_xfjlmxb数据层接口。消费计划明细表
		/// </summary>
		public static YW.IDAL.YWXT.IYW_xfjlmxb CreateYW_xfjlmxb()
		{

			string ClassNamespace = AssemblyPath +".YWXT.YW_xfjlmxb";
			object objType=CreateObject(AssemblyPath,ClassNamespace);
			return (YW.IDAL.YWXT.IYW_xfjlmxb)objType;
		}


		/// <summary>
		/// 创建YW_xfjlzb数据层接口。消费记录主表
		/// </summary>
		public static YW.IDAL.YWXT.IYW_xfjlzb CreateYW_xfjlzb()
		{

			string ClassNamespace = AssemblyPath +".YWXT.YW_xfjlzb";
			object objType=CreateObject(AssemblyPath,ClassNamespace);
			return (YW.IDAL.YWXT.IYW_xfjlzb)objType;
		}


		/// <summary>
		/// 创建yw_zfjlmxb数据层接口。支付记录明细表
		/// </summary>
		public static YW.IDAL.YWXT.Iyw_zfjlmxb Createyw_zfjlmxb()
		{

			string ClassNamespace = AssemblyPath +".YWXT.yw_zfjlmxb";
			object objType=CreateObject(AssemblyPath,ClassNamespace);
			return (YW.IDAL.YWXT.Iyw_zfjlmxb)objType;
		}


		/// <summary>
		/// 创建YW_zfjlzb数据层接口。支付记录主表
		/// </summary>
		public static YW.IDAL.YWXT.IYW_zfjlzb CreateYW_zfjlzb()
		{

			string ClassNamespace = AssemblyPath +".YWXT.YW_zfjlzb";
			object objType=CreateObject(AssemblyPath,ClassNamespace);
			return (YW.IDAL.YWXT.IYW_zfjlzb)objType;
		}

        /// <summary>
        /// 创建yw_hyyeb数据层接口。
        /// </summary>
        public static YW.IDAL.HYXT.Iyw_hyyeb Createyw_hyyeb()
        {

            string ClassNamespace = AssemblyPath + ".HYXT.yw_hyyeb";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (YW.IDAL.HYXT.Iyw_hyyeb)objType;
        }


        /// <summary>
        /// 创建yw_jfbgjl数据层接口。积分变更记录表
        /// </summary>
        public static YW.IDAL.HYXT.Iyw_jfbgjl Createyw_jfbgjl()
        {

            string ClassNamespace = AssemblyPath + ".HYXT.yw_jfbgjl";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (YW.IDAL.HYXT.Iyw_jfbgjl)objType;
        }


        /// <summary>
        /// 创建YW_yebgjl数据层接口。余额变更记录表
        /// </summary>
        public static YW.IDAL.HYXT.IYW_yebgjl CreateYW_yebgjl()
        {

            string ClassNamespace = AssemblyPath + ".HYXT.YW_yebgjl";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (YW.IDAL.HYXT.IYW_yebgjl)objType;
        }


}
}